#!/usr/bin/env python
#-*- coding:utf-8 -*-

class Num(object):
    
    def __init__(self,num=None):
        self.num=num
    
    def numify(self):
        try:
            string=str(self.num)
        except Exception:
            return None
        return ''.join(c for c in string if c.isdigit())
        
    def denumify(self,pattern,char='X'):
        string=self.num
        out=[]
        for c in pattern:
            if c==char:
                out.append(string[0])
                string=string[1:]
            else:
                out.append(c)
        return ''.join(out)
        
    def dateify(self,fmt='XXXX-XX-XX XX:XX:XX'):
        return self.denumify(fmt)
        
    def str2num(self,default=0):
        value=self.num
        if isinstance(value,(int,long,float)):
            return value
        elif isinstance(value,basestring):
            if value.find('.')>-1:
                try:
                    return float(value)
                except (TypeError,ValueError):
                    return default
            else:
                try:
                    return int(value)
                except (TypeError,ValueError):
                    try:
                        return long(value)
                    except (TypeError,ValueError):
                        return default
        return default
    
    def get(self,default=None):
        return self.str2num(default)
        
    def is_num(self):
        num=self.get()
        if num is None:
            return False
        return True
    
    def to36(self):
        num=int(self.get(self.num))
        assert num is not None and num>=0,"Must supply a positive integer."
        letters="0123456789abcdefghijklmnopqrstuvwxyz"
        converted=[]
        while num!=0:
            num,r=divmod(num,36)
            converted.insert(0,letters[r])
        return "".join(converted) or '0'
    
if __name__ == '__main__':
    num=Num('13401145805.0')
    print num.get()
    print num.is_num()
    print num.numify()
    print num.denumify('XXX XXXX XXXX')
    num=Num('20130530144550')
    print num.dateify()
    print num.to36()
